package com.huiquan.vocab.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.vocab.service.VocabElementLabelService;

@Controller
public class VocabElementLabelController {

	@Autowired
	private VocabElementLabelService vocabElementLabelService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/vocab/element_label_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("exactSearchFlag", "0");
			pageParam.put("checkFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		// 获取页面参数，如果request中有值则修改session，否则从session中拿值
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String elementKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "elementKey");
		String checkFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "checkFlag");
		String labelKey = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "labelKey");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		// 显示标签的参数
		if (labelKey != null && !labelKey.isEmpty()) {
			String activeLabel = (String) pageParam.get("activeLabel"); // 当前标签中要高亮显示的列表
			List<List<String>> labelLists = (List<List<String>>) pageParam.get("labelLists"); // 当前标签要显示的完整列表
			activeLabel = getActiveLabelByKey(activeLabel, labelKey, labelLists);
			labelKey = getLabelKeyByActiveLabel(activeLabel); // 真实进入查询的labelKey
			pageParam.put("activeLabel", activeLabel);
			pageParam.put("labelLists", vocabElementLabelService.getLabelListsByKey(labelLists, labelKey));
		} else {
			pageParam.put("activeLabel", "");
			pageParam.put("labelLists", vocabElementLabelService.initLabelLists());
		}

		return vocabElementLabelService.list(start, elementKey, exactSearchFlag, checkFlag, labelKey,countPerPageStr);
	}

	private String getLabelKeyByActiveLabel(String activeLabel) {
		if (activeLabel != null && !activeLabel.isEmpty()) {
			String[] activeArray = activeLabel.split(",");
			return activeArray[activeArray.length - 1];
		}
		return "";
	}

	private String getActiveLabelByKey(String activeLabel, String labelKey, List<List<String>> labelLists) {
		if (labelKey.startsWith("^")) {
			if(activeLabel.indexOf(labelKey.substring(1))>0){
				return activeLabel.substring(0, activeLabel.indexOf(labelKey.substring(1)));
			}else{
				return activeLabel.replace(",", "");
			}
		} else {
			String newActive = "";
			String[] activeArray = activeLabel.split(",");
			for (int i = 0; i < labelLists.size(); i++) {
				if(labelLists.get(i).contains(labelKey)) {
					newActive += labelKey + ",";
					break;
				} else 	if (labelLists.get(i).contains(activeArray[i])) {
					newActive += activeArray[i] + ",";
				}
			}
			
			return newActive;
		}
	}

	@RequestMapping(value = "/vocab/element_label_init.htm")
	public ModelAndView init(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		vocabElementLabelService.init();

		resp.sendRedirect("/vocab/element_label_list.htm?tabid=21_9");

		return null;
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_add_label.do")
	public ReturnData addLabel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String label = req.getParameter("label").trim();

		return vocabElementLabelService.addLabel(label);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_change_label.do")
	public ReturnData changeLabel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String label = req.getParameter("label").trim();

		return vocabElementLabelService.changeLabel(id, label);
	}

	@ResponseBody
	@RequestMapping(value = "/vocab/element_change_quantization.do")
	public ReturnData changeQuantization(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String quantization = req.getParameter("quantization").trim();

		return vocabElementLabelService.changeQuantization(id, quantization);
	}

	@RequestMapping(value = "/vocab/element_label_export.htm")
	public void labelExport(HttpServletRequest req, HttpServletResponse resp) throws Exception {

		vocabElementLabelService.labelExport(resp);
	}
	
	@ResponseBody
	@RequestMapping(value = "/vocab/element_label_import.do")
	public ReturnData labelImport(HttpServletRequest req, @RequestParam(required = false) MultipartFile file)
			throws Exception {

		return vocabElementLabelService.labelImport(file);
	}
	@ResponseBody
	@RequestMapping(value = "/vocab/element_label_change_isdecorateO.do")
	public ReturnData changeisDecorateO(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		String id = req.getParameter("id");
		String isDecorateO = req.getParameter("isDecorateO");

		return vocabElementLabelService.changeisDecorateO(id,isDecorateO);
	}
}
